home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / glibc108.gz / glibc108 / glibc-1.08.1 / sysdeps / mach / hurd / __access.c next >
Encoding:
C/C++ Source or Header  |  1994-05-19  |  3.6 KB  |  119 lines

  1. /* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <unistd.h>
  21. #include <hurd.h>
  22. #include <hurd/port.h>
  23. #include <hurd/id.h>
  24. #include <fcntl.h>
  25.  
  26. /* Test for access to FILE by our real user and group IDs.  */
  27. int
  28. DEFUN(__access, (file, type), CONST char *file AND int type)
  29. {
  30.   error_t err;
  31.   file_t crdir, cwdir, rcrdir, rcwdir, io;
  32.   struct hurd_userlink crdir_ulink, cwdir_ulink;
  33.   int flags;
  34.  
  35.   HURD_CRITICAL_BEGIN;
  36.  
  37.   __mutex_lock (&_hurd_id.lock);
  38.   /* Get _hurd_id up to date.  */
  39.   if (err = _hurd_check_ids ())
  40.     goto lose;
  41.  
  42.   if (_hurd_id.rid_auth == MACH_PORT_NULL)
  43.     {
  44.       /* Set up _hurd_id.rid_auth.  This is a special auth server port
  45.      which uses the real uid and gid (the first aux uid and gid) as
  46.      the only effective uid and gid.  */
  47.  
  48.       if (_hurd_id.aux.nuids < 1 || _hurd_id.aux.ngids < 1)
  49.     {
  50.       /* We do not have a real UID and GID.  Lose, lose, lose!  */
  51.       err = EGRATUITOUS;
  52.       goto lose;
  53.     }
  54.  
  55.       /* Create a new auth port using our real UID and GID (the first
  56.      auxiliary UID and GID) as the only effective IDs.  */
  57.       if (err = __USEPORT (AUTH,
  58.                __auth_makeauth (port,
  59.                         NULL, MACH_MSG_TYPE_COPY_SEND, 0,
  60.                         _hurd_id.aux.uids, 1,
  61.                         _hurd_id.aux.gids, 1,
  62.                         _hurd_id.aux.uids,
  63.                         _hurd_id.aux.nuids,
  64.                         _hurd_id.aux.gids,
  65.                         _hurd_id.aux.ngids,
  66.                         &_hurd_id.rid_auth)))
  67.     goto lose;
  68.     }
  69.  
  70.   /* Get a port to our root directory, authenticated with the real IDs.  */
  71.   crdir = _hurd_port_get (&_hurd_ports[INIT_PORT_CRDIR], &crdir_ulink);
  72.   err = __io_reauthenticate (crdir, _hurd_pid);
  73.   if (!err)
  74.     err = __auth_user_authenticate (_hurd_id.rid_auth,
  75.                     crdir, _hurd_pid, &rcrdir);
  76.   _hurd_port_free (&_hurd_ports[INIT_PORT_CRDIR], &crdir_ulink, crdir);
  77.  
  78.   if (!err)
  79.     {
  80.       /* Get a port to our current working directory, authenticated with
  81.          the real IDs.  */
  82.       cwdir = _hurd_port_get (&_hurd_ports[INIT_PORT_CWDIR], &cwdir_ulink);
  83.       err = __io_reauthenticate (cwdir, _hurd_pid);
  84.       if (!err)
  85.     err = __auth_user_authenticate (_hurd_id.rid_auth,
  86.                     cwdir, _hurd_pid, &rcwdir);
  87.       _hurd_port_free (&_hurd_ports[INIT_PORT_CWDIR], &cwdir_ulink, cwdir);
  88.     }
  89.  
  90.   /* We are done with _hurd_id.rid_auth now.  */
  91.  lose:
  92.   __mutex_unlock (&_hurd_id.lock);
  93.  
  94.   HURD_CRITICAL_END;
  95.  
  96.   if (err)
  97.     return __hurd_fail (err);
  98.  
  99.   /* Now do a path lookup on FILE, using the crdir and cwdir
  100.      reauthenticated with _hurd_id.rid_auth.  */
  101.  
  102.   flags = 0;
  103.   if (type & R_OK)
  104.     flags |= O_READ;
  105.   if (type & W_OK)
  106.     flags |= O_WRITE;
  107.   if (type & X_OK)
  108.     flags |= O_EXEC;
  109.  
  110.   err = __hurd_path_lookup (rcrdir, rcwdir, file, flags, 0, &io);
  111.   __mach_port_deallocate (__mach_task_self (), rcrdir);
  112.   __mach_port_deallocate (__mach_task_self (), rcwdir);
  113.   if (err)
  114.     return __hurd_fail (err);
  115.  
  116.   __mach_port_deallocate (__mach_task_self (), io);
  117.   return 0;
  118. }
  119.